package utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.csvreader.CsvWriter;

public class CsvUtils {
	/**
	 * 导出
	 * 
	 * @author 'wangsm'
	 * @param path
	 * @param response
	 * @return
	 */
	public static Object exportexcel(String path, HttpServletResponse response) {
		File file = new File(path);
		// 导出成功
		String filename = file.getName();
		try {
			filename = new String(filename.getBytes("gbk"), "iso8859-1");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		response.resetBuffer();
		response.setContentType("application/octet-stream; charset=UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename="
				+ filename);
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(file));
			bos = new BufferedOutputStream(response.getOutputStream());
			byte[] buf = new byte[4096];
			int n = 0;
			while ((n = bis.read(buf)) != -1) {
				bos.write(buf, 0, n);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bis != null) {
				try {
					bis.close();
				} catch (Exception e2) {
				}
			}
			if (bos != null) {
				try {
					bos.close();
				} catch (Exception e2) {
				}
			}
			file.delete();// 下载完成后删除文件
		}
		return null;
	}
	
	/**
	 * 创建csv
	 * 
	 * @param csvname csv名称
	 * @param listName listdata中的map的key
	 * @param listdata 数据列表
	 * @param record 列名
	 * @return
	 */
	@SuppressWarnings("finally")
	public static ByteArrayInputStream createCsv1(String csvname,String[] listName,
			List<Map<String,String>> listdata,String[] record){
		CsvWriter writer =null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			writer= new CsvWriter(out, ',',Charset.defaultCharset());
			writer.writeRecord(record);
			for(int i=0;i<listdata.size();i++){
				Map<String,String> map=new HashMap<String,String>();
				map=listdata.get(i);
				String[] data=new String[listName.length];
				for(int j=0;j<listName.length;j++){
					data[j]=map.get(listName[j]);
				}
				writer.writeRecord(data);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			writer.close();
			ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray());
			return bais;
		}
	}
	//创建csv
	public static CsvWriter createCsv(String csvname,String[] listName,
			List<Map<String,String>> listdata,String[] record,boolean isExit,boolean isHead,
			String[] newrecord){
		
		FileWriter buf=null;
		CsvWriter writer =null;
		try {
		buf=new FileWriter(csvname,isExit);
		writer= new CsvWriter(buf, ',');
		if(!isExit){
			writer.writeRecord(record);
		}
		for(int i=0;i<listdata.size();i++){
			Map<String,String> map=new HashMap<String,String>();
			map=listdata.get(i);
			String[] data=new String[listName.length];
			for(int j=0;j<listName.length;j++){
				data[j]=map.get(listName[j]);
			}
			writer.writeRecord(data);
		}
		if(!isHead){
			writer.writeRecord(newrecord);
		}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			writer.close();
			try {
				buf.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	/**
	 * 向csv文件中添加一行
	 * 
	 * @param csvname 文件名称
	 * @param record 行内容
	 * @return
	 */
	public static CsvWriter addRow(String csvname,String[] record){
		if(StringUtils.isEmpty(csvname)){
			return null;
		}
		FileWriter buf=null;
		CsvWriter writer =null;
		try {
			buf=new FileWriter(csvname,true);
			writer= new CsvWriter(buf, ',');
			writer.writeRecord(record);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			writer.close();
			try {
				buf.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
}
